$(function() {
	$("#loading-first").fadeOut(500);
	var page = 1;
	var tddata = [{
		key: 'plate_no',
		type: 1,
		url: ''
	}, {
		key: 'driver_name',
		type: 1,
		url: '{:url("detail")}',
		id: 137
	}, {
		key: 'driver_phone',
		type: 1,
		url: ''
	}, {
		key: 'company_name',
		type: 1,
		url: ''
	}, {
		key: 'accident_time',
		type: 1,
		url: ''
	}, {
		key: 'address',
		type: 1,
		url: ''
	}, {
		key: 'price',
		type: 1,
		url: ''
	} ,{
		key: 'handle_desc',
		type: 1,
		url: ''
	}, {
		key: 'desc',
		type: 1,
		url: ''
	}];
	var addoperation = `<td>
						<div class="tpl-table-black-operation operate">
							
							<a href="javascript:;" class='exit'>
								<i class="am-icon-pencil"></i> 编辑
							</a>
							<a href="javascript:;" class="tpl-table-black-operation-del delete">
								<i class="am-icon-pencil"></i> 删除
							</a>
						</div>
					</td>`
	laydate.render({
	  elem: '.time'
	  ,range: true,
	  done:function(value, date, endDate){
	  	arr = value.split(' - ')
	  	$("input[name='start_time']").val(arr[0])
	  	$("input[name='end_time']").val(arr[1])
	  }
	});
	var data={};
	data.page=page;
	data.pagenum=10
	var where_json={}
	where_json.company_id=['in',$('.company_all_id').val()]
	{if $Think.get.start_time}
	where_json.accident_time=['between time',['{$Think.get.start_time}','{$Think.get.end_time}']]
	{/if}
	{if $Think.get.number}
	where_json.plate_no=['like',"%{$Think.get.number}%"]
	{/if}
	{if $Think.get.handle_status}
	where_json.handle_status=['in',"{$Think.get.handle_status}"]
	{/if}
	data.where_json=JSON.stringify(where_json)
	ajaxaccident(data)

	function ajaxaccident(data) {
		Load()
		$.ajax({
			type: 'post',
			data: data,
			url: '{:url("adminapi/Caraccident/accidentList")}',
			success: function(res) {
				Load(1)
//				res = JSON.parse(res)
				console.log(res)
				for(var stateidx = 0; stateidx < res.list.length; stateidx++) {
					var dispose;
					if(res.list[stateidx].state == 1) {
						res.list[stateidx].state2 = '待处理'
						dispose = `<a href="javascript:;">
								<i class="am-icon-pencil"></i> 处理
							</a>`
					} else if(res.list[stateidx].state == 2) {
						res.list[stateidx].state2 = '已处理'
						dispose = " "
					}
					$('.operate').eq(stateidx).append(dispose)
				}
				$('tbody').html(tbodymainf2(res.list, 1, tddata, addoperation, 'accident_id'))
				$('.pagination').html(pagechang1(res.count, 10, page))
				for(var disposeidx = 0; disposeidx < res.list.length; disposeidx++) {
					var dispose;
					if(res.list[disposeidx].state == 1) {
						dispose = `<a href="javascript:;" class='dispose'>
								<i class="am-icon-pencil"></i> 处理
							</a>`
					} else if(res.list[disposeidx].state == 2) {
						dispose = ""
					}
					$('.operate').eq(disposeidx).append(dispose)
				}
				$('.laquo').on('click', function() {
					page -= 1;
					data.page=page
					console.log(page)
					ajaxaccident(data)
				})
				$('.pageli').on('click', function() {
					page = $(this).html()
					data.page=page
					ajaxaccident(data)
				})
				//下一页
				$('.raquo').on('click', function() {
					page += 1;
					data.page=page
					ajaxaccident(data)
				});
				$('.car_rental').on('click', function() {
					choosecar('{:url("rent/index")}?op=search')
				});
				$('.delete').on('click',function(){
						var deletedata={
							accident_ids:$(this).closest('tr').data('id')
						}
						deleteindex=layer.open({
							type: 1,
							title: false,
							id: "delete" ,
							content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
							btn: ['确定', '取消'],
							btnAlign: 'c' //按钮居中
								,
							shade: 0 //不显示遮罩
								,
							yes: function() {
								var deleteload = layer.load(1, {
									shade: [0.1, '#fff'] //0.1透明度的白色背景
								});
								$.ajax({
									method: "POST",
									url: '{:url("adminapi/Caraccident/delete")}',
									data: deletedata,
									success: function(res) {
										if(res.code == 1) {
											console.log(data)
											Msg('删除成功')
											ajaxaccident(data)
										} else if(res.code == 0) {
											
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
										layer.close(deleteload);
										layer.close(deleteindex);
			
									},
									error:function(){
										layer.close(deleteload);
											layer.close(deleteindex);
										Err('请求失败')
									}
								});
			
							},
							btn2: function() {
								layer.closeAll();
							}
						});
					})
				$('.detail').on('click', function() {
					layer.open({
						type: 2,
						title: '事故详情',
						shadeClose: true,
						shade: 0.8,
						area: ['30%', '80%'],
						content: '{:url("accidentdetail")}?op=search' //iframe的url
					});
				})
				$('.exit').on('click', function() {
					window.location.href = '{:url("accidentadd")}?id='+ $(this).closest('tr').data('id')
				})
				$('.dispose').on('click', function() {
					disposeopen(1)
				})
			}
		})
	}

	function disposeopen(id){
		console.log(id)
		var load = layer.load(0, {
			shade: [0.3, '#fff']
		});

		layer.close(load);
		var index = layer.open({
			title: false,
			type: 1,
			skin: 'layui-layer-rim', //加上边框
			area: ['550px', '300px'], //宽高
			content: `<div class="am-u-sm-12" style="padding:20px">
						<form class="am-form-inline tpl-form-border-form" enctype="multipart/form-data" data-ajax="1" data-check="1">
						<div class="am-u-sm-8 cl-p " >
							<div class='am-u-sm-12 margin_bottom_10'>
								<label class="am-u-sm-4 am-form-label cl-p" style="line-height: 35px;font-size: 14px;">损失： </label>
								
								<div class="am-u-sm-8 cl-p">
									<input type="text" name="number" value="{$item.number}" class="tpl-form-input" style="line-height: 21px;" data-check='require' data-msg=""/>
								</div>
							</div>
							<div class='am-u-sm-12'>
							<label class="am-u-sm-4 am-form-label cl-p" style="line-height: 35px;font-size: 14px;">处理结果说明： </label>
								<div class='am-u-sm-8 cl-p'>
									<textarea class="cl-p" style='height:150px'></textarea>
								</div>
							</div>
							<div class='am-u-sm-12' style='text-align:center;margin-top:5px'>
								<button type='button' class='am-btn-sm am-btn am-btn-primary sum'>提交</button>
								<button type='button' class='am-btn-sm am-btn am-btn-primary close'>取消</button>
							</div>
						</div>
						<div class='am-u-sm-4 cl-p'>
							<div class="am-u-sm-12">
								<div class="am-u-sm-12 ">
									<div class="am-form-group file-w">
										<div style="width: 150px;height: 150px;">
											<img class="fileimg" data-magnify="1" src="{if $item.driving_license_img2}{:CxImg($item.driving_license_img2)}{else/}{$static_dir}/assets/img/noimg.gif{/if}" style="width: 100%;height: 100%;" />
										</div>
										<button type="button" data-type='file' class="am-btn am-btn-danger am-btn-sm "><i class="am-icon-cloud-upload"></i> 添加处理凭证</button>
										<input type="file" name="driving_license_img2" data-type='img' style="display: none;">
									</div>
								</div>
							</div>
						</div>
						
						
						</form>
					</div>`,
			success: function(res) {
				$('.sum').on('click',function(){
					layer.close(index)
				})
				$('.close').on('click',function(){
					layer.close(index)
				})
			}
		});

	}
})